package com.example.weibo.service;

import org.apache.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.example.weibo.model.Weibo;
import com.example.weibo.repository.WeiboRepository;

@Service
public class WeiboService {

    @Autowired
    private WeiboRepository weiboRepository;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;  // 使用 RedisTemplate

    @GlobalTransactional(timeoutMills = 300000, name = "spring-seata-tx")
    public void publishWeibo(String content) {
        // 写入 MySQL 数据库
        Weibo weibo = new Weibo();
        weibo.setContent(content);
        weibo.setStatus("上线");
        weiboRepository.save(weibo);

        // 写入 Redis
        redisTemplate.opsForValue().set(weibo.getId().toString(), content);
    }
}
